<template>
	<div class="system-combo-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px"
			:close-on-click-modal="false">
			<el-form ref="comboDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" class="mb20">
						<el-form-item label="套餐名称">
							<el-input v-model="state.ruleForm.title" placeholder="请输入套餐名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="套餐状态">
							<el-switch v-model="state.ruleForm.status" :active-value="1" :inactive-value="0" inline-prompt
								active-text="启" inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="套餐描述">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入套餐描述" clearable type="textarea"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="渠道">
							<el-tree ref="platformsRef"  :default-checked-keys="state.selcetPlatforms" accordion :data="state.platforms" check-strictly @check="getCurrentNode" show-checkbox node-key="id" :props="defaultProps" class="menu-data-tree"/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { comboApi } from '/@/api/combo/index';
import { commApi } from '/@/api/comm/index';
// 定义变量内容
const comboDialogFormRef = ref();
const platformsRef = ref();
const defaultProps = {
	children: 'children',
	label: 'cname'
}
const state = reactive({
	selcetPlatforms:[],
	platforms: [],
	ruleForm: {
		title: '', // 套餐名称
		status: 1, // 套餐状态
		remark: '',
		platform:'',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type, row) => {
	state.dialog.type = type
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.selcetPlatforms = state.ruleForm.platform.split(',')
		state.dialog.title = '修改套餐';
		state.dialog.submitTxt = '修 改';
	} else {
		state.selcetPlatforms = []
		state.dialog.title = '新增套餐';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			title: '', // 套餐名称
			status: 1, // 套餐状态
			remark: '',
			platform:'',
		}
	}
	state.dialog.isShowDialog = true;
	getPlats()
};
//监听点击tree 
const getCurrentNode = (data) => {
	const node = platformsRef.value.getNode(data);
	parentNodes(node);
};
// 未使用   点击复选框选中子组件
const childNodes = (node) => {
	const len = node.childNodes.length;
	for (let i = 0; i < len; i += 1) {
		node.childNodes[i].checked = node.checked;
		childNodes(node.childNodes[i]);
	}
};
// 点击复选框选中父组件
const parentNodes = (node) => {
	if (node.parent) {
		for (const key in node) {
			if (key === 'parent') {
				node[key].checked = true;
				parentNodes(node[key]);
			}
		}
	}
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	let tree = platformsRef.value.getCheckedKeys();
	state.ruleForm.platform = tree.join(",")
	if (state.dialog.type === 'add') {
		comboApi().add(state.ruleForm).then(async res => {
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	} else {
		comboApi().edit(state.ruleForm).then(async res => {
			closeDialog(); // 关闭弹窗
			emit('refresh');
		})
	}
};

const getPlats = () => {
	commApi().getPlats().then(res=>{
		state.platforms = res.data.paltform
	})
}

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
:deep(.el-input-number .el-input__inner) {
	text-align: left;
}
.menu-data-tree {
	width: 100%;
	border: 1px solid var(--el-border-color);
	border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
	padding: 5px;
}
</style>
